package progress.exa30_1;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

public class ClassObjectStudy {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        System.out.println(createClass1().getName());
        System.out.println(createClass2().getName());
        System.out.println(createClass3().getName());
        equalsClassTest();
        createInstance();
    }

    public static Class<?> createClass1(){
        Object o = new Object();
        return o.getClass();
    }

    public static Class<?> createClass2() throws ClassNotFoundException {
        String className="java.util.Random";
        Class<?> cl = Class.forName(className);//这里的className的内容必须类名或接口名才有效
        return cl;
    }

    public static Class<?> createClass3(){
        return Object.class;
    }

    public static void equalsClassTest(){
        Class<?> oneClass = Object.class;
        Class<?> twoClass = Integer.class;
        Class<?> three = new Object().getClass();
        System.out.println(oneClass==twoClass);
        System.out.println(oneClass==three);
    }

    public static void createInstance() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class cl = ArrayList.class;
        ArrayList one = (ArrayList) cl.newInstance();
        one.add(10);
        ArrayList two = (ArrayList) cl.getConstructor().newInstance();
        two.add(11);
        System.out.println(one);
        System.out.println(two);
    }

}
